﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DQYT.HTDZ.BLL;
using DQYT.HTDZ.Model;
using System.IO;

namespace SkWeb.Handlers
{
    public partial class OperateHandler : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string op = Request.QueryString["op"];
            string success = "{\"success\":true}";
            string result = success;
            string msg = string.Empty;

            string fid = string.Empty;
            string catalogName = string.Empty;
            TB_Template_Menu_Model parentModel = null;

            TB_Template_Menu_BLL templateBLL = new TB_Template_Menu_BLL();

            switch (op)
            {
                case "deleteresourcefile":
                    string id = Request.Form["tmpid"];
                    if (!templateBLL.DeleteItem(int.Parse(id), ref msg))
                    {
                        result = string.Concat("{\"success\":false, \"msg\":\"", msg, "\"}");
                    }
                    break;
                case "addresourcefile":

                    HttpPostedFile fileData = Request.Files["Filedata"];
                    if (fileData != null)
                    {
                        fid = Request.Params.Get("fid");
                        parentModel = templateBLL.QueryItem(int.Parse(fid));
                        string fileName = fileData.FileName;
                        string tempDirectoryPath = string.Concat(AppDomain.CurrentDomain.BaseDirectory,
                            "SystemManage\\", parentModel.TMP_URL);
                        if (!Directory.Exists(tempDirectoryPath))
                        {
                            Directory.CreateDirectory(tempDirectoryPath);
                        }
                        string filePath = string.Concat(AppDomain.CurrentDomain.BaseDirectory, 
                            "SystemManage\\", parentModel.TMP_URL, "\\", fileName);
                        fileData.SaveAs(filePath);

                        if (!templateBLL.AddItem(new TB_Template_Menu_Model
                        {
                            TMP_MC = fileName,
                            _parentId = parentModel.TMP_ID,
                            TMP_URL = string.Concat(parentModel.TMP_URL, "\\", fileName),
                            TYP_CODE = 30
                        }))
                        {
                            result = string.Concat("{\"success\":false, \"msg\":\"", "系统异常，请联系管理员！", "\"}");
                        }
                    }
                    else
                    {
                        result = string.Concat("{\"success\":false, \"msg\":\"", "请选择要上传的文件！", "\"}");
                    }
                    
                    break;
                case "addresourcecatalog":
                    fid = Request.Form["fid"];
                    parentModel = templateBLL.QueryItem(int.Parse(fid));
                    catalogName = Request.Form["cataname"];
                    string directoryPath = string.Concat(AppDomain.CurrentDomain.BaseDirectory, "\\SystemManage\\", parentModel.TMP_URL, "\\", catalogName);
                    if (!Directory.Exists(directoryPath))
                        Directory.CreateDirectory(directoryPath);
                    if(!templateBLL.AddItem(new TB_Template_Menu_Model
                    {
                        TMP_MC = catalogName,
                        _parentId = parentModel.TMP_ID,
                        TMP_URL = string.Concat(parentModel.TMP_URL, "\\", catalogName),
                        TYP_CODE = 20
                    }))
                    {
                        result = string.Concat("{\"success\":false, \"msg\":\"", "系统异常，请联系管理员！", "\"}");
                    }
                    break;
            }
            Response.ContentType = "text/json";
            Response.Write(result);
        }
    }
}